package cc.geguo.settree.setdemo;

import java.util.HashSet;

/**
 * 作者：13687
 * 日期：2017/6/18
 * 项目：JavaSetTree
 * 描述：验证唯一性
 */
public class HaseSetTest {
	public static void main(String[] args) {
		HashSet hashSet = new HashSet();
		hashSet.add(new Person("geguo",18));
		hashSet.add(new Person("geguo",19));
		hashSet.add(new Person("geguo",20));
		hashSet.add(new Person("geguo",20));

		System.out.println(hashSet);
	}
}

class Person{
	public String name;
	public int age;

	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	@Override
	public int hashCode() {
		System.out.println("hash========="+name);
		return name.hashCode()+age*12;
	}



	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Person)){
			return false;
		}
		System.out.println(this.name+ " .... "+((Person) obj).name);

		if (this.name.equals(((Person) obj).name)&&this.age == ((Person) obj).age)
			return true;
		else return false;
	}
}
